﻿/************************************************************************
 * SMLib - State Model Library
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     SMLib\Interoperability\DOT\DotHelper.cs
 * Description  :     DOT format helper
 * Change log   :     -1 Sep 2013:  Initial Version
 * To Do        :     
 ************************************************************************/
using System;
using System.Collections.Generic;
using SMLib.Base;
using System.IO;

namespace SMLib.Interoperability.DOT
{
    #region -=[ DOT ELEMENTS ]=-
    /// <summary>
    /// Elements in DOT File
    /// </summary>
    public class DotElement
    {
        //* nothing
    }

    /// <summary>
    /// DOT Transition (include a sourse, a destination, and a label of transition)
    /// </summary>
    public class DotTransition : DotElement
    {
        string source;
        string destination;
        string label;


        /// <summary>
        /// Constructor
        /// </summary>
        public DotTransition()
        {
            source = null;
            destination = null;
            label = null;
        }

        public DotTransition(string source, string destination, string label)
        {
            this.source = source;
            this.destination = destination;
            this.label = label;
        }

        public static DotTransition Parse(string dotStr)
        {
            throw new NotImplementedException();
        }

        public override string ToString()
        {
            return base.ToString();
        }


        public string Source
        {
            get { return source; }
        }

        public string Destination
        {
            get { return destination; }
        }



        public string Label
        {
            get { return label; }
        }



    }
    #endregion

    /// <summary>
    /// DOT format helper
    /// </summary>
    public class DotHelper<StateType,TransitionType>
    {
        public void WriteToDot(string path, IDiGraph<StateType,TransitionType> ss)
        {
            //bool addLabelTransiton = true;
            string DotString = "Digraph StateSpace{\r\n";
            foreach (StateType st in ss.GetStates())
            {
            	foreach (TransitionType tr in ss.TransitionsOf(st))
                {
            		DotString += VertexToVertexID(st) + " -> " + VertexToVertexID(ss.DestinationOf(tr)) + "[label=\"" + EdgeToEdgeID(tr) + "\"];\r\n";
                    //if (addLabelTransiton)
                    //    DotString += NodeLabel(st) + " -> " + NodeLabel(tr.Destination) + "[label=\"" + TransitionLabel(tr) + "\"];\r\n";
                    //else
                    //    DotString += NodeLabel(st) + " -> " + NodeLabel(tr.Destination) + ";\r\n";
                }
            }
            DotString += "}";
            File.WriteAllText(path, DotString);
        }

        /*
        public void WriteToDot<STATETYPE, TRANSITIONTYPE>(string path, IDiGraph<STATETYPE, TRANSITIONTYPE> ss)
            //where STATETYPE : IState<TRANSITIONTYPE>
            //where TRANSITIONTYPE : ITransition<STATETYPE>
        {
            //bool addLabelTransiton = true;
            string DotString = "Digraph StateSpace{\r\n";
            foreach (STATETYPE st in ss.GetStates())
            {
                foreach (TRANSITIONTYPE tr in st.Transitions)
                {
                    DotString += VertexToVertexID<STATETYPE, TRANSITIONTYPE>(st) + " -> " +
                        VertexToVertexID<STATETYPE, TRANSITIONTYPE>(tr.Destination) + "[label=\"" + EdgeToEdgeID<STATETYPE>(tr) + "\"];\r\n";
                    //if (addLabelTransiton)
                    //    DotString += NodeLabel(st) + " -> " + NodeLabel(tr.Destination) + "[label=\"" + TransitionLabel(tr) + "\"];\r\n";
                    //else
                    //    DotString += NodeLabel(st) + " -> " + NodeLabel(tr.Destination) + ";\r\n";
                }
            }
            DotString += "}";
            File.WriteAllText(path, DotString);
        }
        

        public TranSys ReadFromFile(string path)
        {
            throw new NotImplementedException();
        }

		*/
        #region -=[ To Be Overrided ]=-

        /// <summary>
        /// Convert a vertex to string (for export porpuse)
        /// </summary>
        /// <param name="state">the state</param>
        /// <returns>the string</returns>
        protected virtual string VertexToVertexID(IStateContainer state)
        {
            return state.Identifier.ToString();
        }

        /// <summary>
        /// Converting an edge to string (for export porpuse)
        /// </summary>
        /// <param name="tr">the transition</param>
        /// <returns>the equivalent string</returns>
        protected virtual string EdgeToEdgeID(TransitionType tr)
        {
            return tr.ToString();
        }

        /// <summary>
        /// Convert a string into the proper transition (for import porpose)
        /// </summary>
        /// <param name="tr">the string</param>
        /// <returns>the equivalent transition</returns>
        protected virtual TransitionType EdgeIdToEge(string tr)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Convert a string into a state (for import porpose)
        /// </summary>
        /// <param name="st">the string</param>
        /// <returns>the equivalent state</returns>
        protected virtual IStateContainer VertexIdToVertex(string st)
        {
            throw new NotImplementedException();
        }



        /// <summary>
        /// Convert a vertex to string (for export porpuse)
        /// </summary>
        /// <param name="state">the state</param>
        /// <returns>the string</returns>
        protected virtual string VertexToVertexID(StateType state)
            //where STATETYPE : IState<TRANSITIONTYPE>
            //where TRANSITIONTYPE : ITransition<STATETYPE>
        {
            return state.ToString();
        }

        /// <summary>
        /// Converting an edge to string (for export porpuse)
        /// </summary>
        /// <param name="tr">the transition</param>
        /// <returns>the equivalent string</returns>
        protected virtual string EdgeToEdgeID<STATETYPE>(TransitionType tr)
        {
            return tr.ToString();
        }

                #endregion

    }
}
